//
//  MultipleCustomerViewController.m
//  GAMCAfterSales
//
//  Created by BigAmazing on 16/5/15.
//  Copyright © 2016年 glendale. All rights reserved.
//
#define kAlphaNum @"0123456789."
#import "MultipleCustomerViewController.h"
#import "MJRefresh.h"
#import "MyAFNetWorkManager.h"

@interface MultipleCustomerViewController (){
    //    车主 from-to
    NSInteger toZ;
    NSInteger fromZ;
}
@property(nonatomic,strong)UITextField * nameTextField;//姓名
@property(nonatomic,strong)UITextField * phoneTextField;//手机号
@property(nonatomic,strong)UITextField * numberTextField;//客户编号
@end

@implementation MultipleCustomerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    toZ = 10;
    
    self.tableViewDataArrS = [[NSMutableArray alloc]init];
    
    _MDic = [[NSMutableDictionary alloc]init];
    [self basicView];   //基本
    [self loadtitlelabelPicture];
    [self createTableView];//创建表视图
    
    //客户姓名
   
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, CGRectGetMaxY(self.detailTableView.frame)+20, 90, 30)];
    nameLabel.text = @"客户名称:";
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.font = [UIFont fontWithName:@"ArialMT" size:18];
    nameLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:nameLabel];
    
    _KHXMlabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame), CGRectGetMaxY(self.detailTableView.frame)+20, 150, 30)];
//    _KHXMlabel.text = @"客户名称:";
    _KHXMlabel.backgroundColor = [UIColor clearColor];
    _KHXMlabel.textColor = [UIColor grayColor];
    _KHXMlabel.font = [UIFont fontWithName:@"ArialMT" size:16];
    _KHXMlabel.layer.borderWidth = 0.9;
    _KHXMlabel.layer.borderColor = [UIColor blackColor].CGColor;
    [self.view addSubview:_KHXMlabel];
    
//    客户编号
    UILabel *NOLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_KHXMlabel.frame), CGRectGetMaxY(self.detailTableView.frame)+20, 90, 30)];
    NOLabel.text = @"客户编号:";
    NOLabel.backgroundColor = [UIColor clearColor];
    NOLabel.textColor = [UIColor grayColor];
    NOLabel.font = [UIFont fontWithName:@"ArialMT" size:18];
    NOLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:NOLabel];
    
    _KHBHlabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(NOLabel.frame), CGRectGetMaxY(self.detailTableView.frame)+20, 150, 30)];
    //    _KHBHlabel.text = @"客户名称:";
    _KHBHlabel.backgroundColor = [UIColor clearColor];
    _KHBHlabel.textColor = [UIColor grayColor];
    _KHBHlabel.font = [UIFont fontWithName:@"ArialMT" size:16];
    _KHBHlabel.layer.borderWidth = 0.9;
    _KHBHlabel.layer.borderColor = [UIColor blackColor].CGColor;
    [self.view addSubview:_KHBHlabel];
    
    //输入金额
//    付款金额：
    UILabel *fukuanLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_KHBHlabel.frame)+30, CGRectGetMaxY(self.detailTableView.frame)+20, 90, 30)];
    fukuanLabel.text = @"付款金额:";
    fukuanLabel.backgroundColor = [UIColor clearColor];
    fukuanLabel.textColor = [UIColor grayColor];
    fukuanLabel.font = [UIFont fontWithName:@"ArialMT" size:18];
    fukuanLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:fukuanLabel];
    
    
    
    _textfield =[[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(fukuanLabel.frame), CGRectGetMaxY(self.detailTableView.frame)+20, 150, 30)];
    _textfield.backgroundColor = [UIColor whiteColor];
    _textfield.textAlignment = NSTextAlignmentCenter;
    _textfield.layer.borderWidth = 0.9;
    _textfield.layer.borderColor = [UIColor blackColor].CGColor;
    _textfield.delegate = self;
    _textfield.font = [UIFont systemFontOfSize:15];
  
    [self.view addSubview:_textfield];
    if ([_flag isEqualToString:@"edit"]) {
        _textfield.text = _moneyStr;
        _KHXMlabel.text = _nameStr;
        _KHBHlabel.text = _numberStr;
    }
    
    //可拆分金额
//    UILabel *kcfLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];
//    kcfLabel.backgroundColor = [UIColor whiteColor];
//    kcfLabel.text = [NSString stringWithFormat:@"%d元",_KCFmoney];
//    kcfLabel.textAlignment = NSTextAlignmentCenter;
//    [kcfLabel setFont:[UIFont systemFontOfSize:15]];
//    [self.view addSubview:kcfLabel];

    
        UIButton * queryButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_textfield.frame)+20, CGRectGetMaxY(self.detailTableView.frame)+20, 75, 30)];
        [queryButton setBackgroundImage:[UIImage imageNamed:@"ht05-1.png"] forState:UIControlStateNormal];
        [queryButton setTitle:@"完  成" forState:UIControlStateNormal];
        [queryButton setTintColor:[UIColor whiteColor]];
        queryButton.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:15];
        [queryButton addTarget:self action:@selector(query) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:queryButton];
    
    __unsafe_unretained UITableView *table1 = self.detailTableView;
    table1.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //        [self.tableViewDataArrS removeAllObjects];
        toZ = 10;
        [self VehicleQuery];
        
        [table1.mj_header endRefreshing];
    }];
    
    
    table1.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        toZ = toZ +10;
        [self VehicleQuery];
        [table1.mj_footer endRefreshing];
    }];
}
-(void)createTableView{
    
    self.detailTableView = [[UITableView alloc]initWithFrame:CGRectMake(60, 255, 918, 280) style:UITableViewStylePlain];
    [self.detailTableView setDataSource:self];
    [self.detailTableView setDelegate:self];
    [self.view addSubview:self.detailTableView];


}
-(void)basicView{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    [backButton setFrame:CGRectMake(50, 30, 45, 60)];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    // Do any additional setup after loading the view.
    
//    UIButton * queryButton = [[UIButton alloc]initWithFrame:CGRectMake(900, 680, 75, 35)];
//    [queryButton setBackgroundImage:[UIImage imageNamed:@"ht05-1.png"] forState:UIControlStateNormal];
//    [queryButton setTitle:@"完  成" forState:UIControlStateNormal];
//    [queryButton setTintColor:[UIColor whiteColor]];
//    queryButton.titleLabel.font = [UIFont fontWithName:@"ArialMT" size:15];
//    [queryButton addTarget:self action:@selector(query:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:queryButton];
    
    UILabel *jiedai = [[UILabel alloc]initWithFrame:CGRectMake(200, 45, 200, 40)];
    jiedai.text = @"多客户结算";
    jiedai.backgroundColor = [UIColor clearColor];
    jiedai.textColor = [UIColor grayColor];
    jiedai.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:jiedai];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 1000, 11)];
    image.image = [UIImage imageNamed:@"渐变分割线.png"];
    [self.view addSubview:image];
    
//    客户名称
    UILabel *customer = [[UILabel alloc]initWithFrame:CGRectMake(65, 120, 90, 30)];
    customer.text = @"客户名称:";
    customer.backgroundColor = [UIColor clearColor];
    customer.textColor = [UIColor grayColor];
    customer.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.view addSubview:customer];
    
    self.nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(customer.frame), 120, 150, 30)];//客户名称
    self.nameTextField.borderStyle = UITextBorderStyleLine;
    self.nameTextField.textColor = [UIColor grayColor];
    self.nameTextField.font = [UIFont fontWithName:@"ArialMT" size:14];
    [self.nameTextField setTextAlignment:NSTextAlignmentCenter];
    self.nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:self.nameTextField];
    
  
  
    
//    手机号
    UILabel *phone = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.nameTextField.frame)+30, 120, 90, 30)];
    phone.text = @"手       机:";
    phone.backgroundColor = [UIColor clearColor];
    phone.textColor = [UIColor grayColor];
    phone.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.view addSubview:phone];
    
    self.phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(phone.frame), 120, 150, 30)];
    self.phoneTextField.borderStyle = UITextBorderStyleLine;
    self.phoneTextField.textColor = [UIColor grayColor];
    self.phoneTextField.font = [UIFont fontWithName:@"ArialMT" size:14];
    [self.phoneTextField setTextAlignment:NSTextAlignmentCenter];
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:self.phoneTextField];

//    客户编号
    UILabel *number = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.phoneTextField.frame)+30, 120, 90, 30)];
    number.text = @"客户编号:";
    number.backgroundColor = [UIColor clearColor];
    number.textColor = [UIColor grayColor];
    number.font = [UIFont fontWithName:@"ArialMT" size:16];
    [self.view addSubview:number];
    
    
    self.numberTextField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(number.frame), 120, 150, 30)];
    self.numberTextField.borderStyle = UITextBorderStyleLine;
    self.numberTextField.textColor = [UIColor grayColor];
    self.numberTextField.font = [UIFont fontWithName:@"ArialMT" size:14];
    [self.numberTextField setTextAlignment:NSTextAlignmentCenter];
    self.numberTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.numberTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.view addSubview:self.numberTextField];
    
    
//    查询按钮
    UIButton *vehiclequery = [[UIButton alloc]initWithFrame:CGRectMake(913, 120, 58, 31)];
    [vehiclequery setTitle:@"查 询" forState:UIControlStateNormal];
    [vehiclequery setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [vehiclequery setBackgroundImage:[UIImage imageNamed:@"ht05-1.png"] forState:UIControlStateNormal];
    [vehiclequery addTarget:self action:@selector(VehicleQuery) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:vehiclequery];
}
-(void)loadtitlelabelPicture
{
    NSMutableArray *labelMutabelArray = [[NSMutableArray alloc]initWithObjects:@"录入日期",@"客户编号",@"客户名称",@"客户性质",@"联系人",@"手机号",@"联系电话",@"  联系地址", nil];
    for(int i = 0; i < 8;i++)
    {
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake((60+i*114), 215, 120, 40)];
        titlelabel.backgroundColor = [[UIColor alloc]initWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        titlelabel.text = [labelMutabelArray objectAtIndex:i];
        titlelabel.textColor = [UIColor grayColor];
        titlelabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:16];
        [titlelabel setTextAlignment:NSTextAlignmentCenter];
        [self.view addSubview:titlelabel];
    }
}
#pragma mark----------buttonClick
//back
-(void)back{

    [self.navigationController popViewControllerAnimated:YES];
}
-(void)query{
    if (_KHBHlabel.text.length == 0) {
        UIAlertView * AV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请选择车主" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
        [AV show];
    }
    else if (_textfield.text.length == 0){
        UIAlertView * AV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请输入金额" delegate:nil cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
        [AV show];
    }
    else{
        if ([_flag isEqualToString:@"add"]) {
            NSMutableDictionary * addDic = [[NSMutableDictionary alloc]init];
            [addDic setValue:_KHXMlabel.text forKey:@"name"];
            [addDic setValue:_KHBHlabel.text forKey:@"NO"];
            [addDic setValue:_textfield.text forKey:@"money"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"add" object:addDic];
            [self.navigationController popViewControllerAnimated:YES];
        }
        if ([_flag isEqualToString:@"edit"]) {
            NSMutableDictionary * addDic = [[NSMutableDictionary alloc]init];
            [addDic setValue:_KHXMlabel.text forKey:@"name"];
            [addDic setValue:_KHBHlabel.text forKey:@"NO"];
            [addDic setValue:_textfield.text forKey:@"money"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"edit" object:addDic];
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }
   
   

}
//查询
-(void)VehicleQuery{
      NSMutableDictionary * parms = [[NSMutableDictionary alloc]init];
    
    if (self.nameTextField.text.length !=0) {
        NSString * str = [self.nameTextField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [parms setValue:str forKey:@"CUSTOMER_NAME"];
    }
    if (self.phoneTextField.text.length != 0) {
        NSString * str = [self.phoneTextField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [parms setValue:str forKey:@"MOBILE"];
    }
    if (self.numberTextField.text.length != 0) {
        NSString * str = [self.numberTextField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [parms setValue:str forKey:@"OWNER_NO"];
    }
    
    [parms setValue:@"1" forKey:@"FROM"];
    [parms setValue:[NSString stringWithFormat:@"%d",toZ] forKey:@"TO"];
    NSLog(@"self.customerQueryDic : %@",parms);
    
    [MyAFNetWorkManager postWithParam:parms andAction:@"QUERY_OWNER" finish:^(NSData *data, NSDictionary *obj, NSError *error) {
       
        if (error == nil)
        {
            NSLog(@"车主查询 json is %@",obj);
            NSDictionary *dic = obj;
            if([[dic valueForKey:@"SUCCESS"] integerValue]==1)
            {
                NSArray *array = [dic objectForKey:@"TM_OWNER"];
                [self.tableViewDataArrS removeAllObjects];
                [self.tableViewDataArrS addObjectsFromArray:array];
                [self.detailTableView reloadData];
                
                
            }
        }
        else{
            
        }
        
    }];

}
#pragma mark-------------tableViewDelegate  tableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView == self.detailTableView)
    {
        
        return self.tableViewDataArrS.count;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];
  
        //        车主
        static NSString *indetfire = @"cell";
        InformationCellS *cell = (InformationCellS *)[tableView dequeueReusableCellWithIdentifier:indetfire];
        if(nil == cell)
        {
            
            cell = [[InformationCellS alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetfire];
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        }
        
        cell.cellLabelOne.text = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"CREATE_DATE"];
        cell.cellLabelTwo.text = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"CUSTOMER_ID"];
        cell.cellLabelThree.text = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"CUSTOMER_NAME"];
        
        //        NSNumber *nature = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"NATURE_NAME"];
        //        cell.cellLabelFour.text = nature.stringValue;
        NSString * nature = [[[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"NATURE_NAME"] stringValue];
        if ([nature isEqualToString:@"11901002"]) {
            cell.cellLabelFour.text = @"个人";
        }
        else if ([nature isEqualToString:@"11901001"]){
            
            cell.cellLabelFour.text = @"公司";
        }
        else{
            cell.cellLabelFour.text = @"";
        }
        
        
        cell.cellLabelFive.text = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"CONTACT_MAN"];
        cell.cellLabelSix.text = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"MOBILE"];
        cell.cellLabelSeven.text = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"TEL"];
        cell.cellLabelEight.text = [[self.tableViewDataArrS objectAtIndex:row]objectForKey:@"ADDRESS"];
        
        return cell;
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    _KHBHlabel.text = [self.tableViewDataArrS[indexPath.row] objectForKey:@"CUSTOMER_ID"];
     _KHXMlabel.text = [self.tableViewDataArrS[indexPath.row] objectForKey:@"CUSTOMER_NAME"];
    
    
}
#pragma textField Delegate


//限制密码内容只能为字母大小写 和 数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == _textfield) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
        
        
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
        
        BOOL canChange = [string isEqualToString:filtered];
        if (string.length == 0) {
            return YES;
        }
        //    return textField.text.length>=12?NO: canChange;
        return canChange;
        
    }else{
        return YES;
    }
}
@end
